package com.srit.core.ibatis.page;

import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;

/**
 * 装载oracle 物理分页方法
 * 
 * @author guan
 * 
 */
public class XSqlMapClientDaoSupport extends SqlMapClientDaoSupport {
	Logger log = LoggerFactory.getLogger(XSqlMapClientDaoSupport.class);

	/**
	 * chenyanji.修正JDK时区问题
	 */
	public XSqlMapClientDaoSupport() {
		TimeZone.setDefault(TimeZone.getTimeZone("ETC/GMT-8"));
	}

	private SqlExecutor sqlExecutor;

	public SqlExecutor getSqlExecutor() {
		return sqlExecutor;
	}

	public void setSqlExecutor(SqlExecutor sqlExecutor) {
		this.sqlExecutor = sqlExecutor;
	}

	public void setEnableLimit(boolean enableLimit) {
		if (sqlExecutor instanceof XSqlExecutor) {
			((XSqlExecutor) sqlExecutor).setEnableLimit(enableLimit);
		}
	}

	/**
	 * 将封装过的sqlExecutor对象SET给getSqlMapClientTemplate，从而改变继承sqlExecutor对象。
	 * 
	 * @throws Exception
	 */

	public void initialized() {
		if (sqlExecutor != null) {
			SqlMapClient sqlMapClient = getSqlMapClientTemplate()
					.getSqlMapClient();
			if (sqlMapClient instanceof ExtendedSqlMapClient) {
				ReflectUtil.setFieldValue(
						((ExtendedSqlMapClient) sqlMapClient).getDelegate(),
						"sqlExecutor", SqlExecutor.class, sqlExecutor);
			}
		} else {
			logger.info("null OracleLimit initialized.");
		}
	}
}
